import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:xunlongchi/bloc/time_bloc.dart';
import 'package:xunlongchi/widgets/animated_roate.dart';

import 'center_taiji.dart';
import 'compass/compass.dart';
import 'organ.dart';
import 'point_line.dart';
import 'round_hour.dart';
import 'round_sed.dart';
import 'shichen.dart';

///首页
class HomePage extends StatelessWidget {
  const HomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        BlocProvider.of<TimeBloc>(context).close();
        SystemNavigator.pop();
        return Future.value(true);
      },
      child: Scaffold(
        backgroundColor: Colors.black,
        body: Center(
          child: Stack(
            alignment: Alignment.center,
            children: [
              //指针
              const PointLine(),
              //罗盘
              const Compass(),
              //中心太极图标
              const CenterTaiji(),
              //时辰
              BlocBuilder<TimeBloc, TimeMag>(
                buildWhen: (p, c) => p.nowTime.hour != c.nowTime.hour,
                builder: (_, time) => _buildShi(time.nowTime.hour),
              ),
              //小时
              BlocBuilder<TimeBloc, TimeMag>(
                buildWhen: (p, c) => p.nowTime.hour != c.nowTime.hour,
                builder: (_, time) => _buildHour(time.nowTime.hour),
              ),
              //分
              Transform.scale(
                scale: 0.92,
                child: BlocBuilder<TimeBloc, TimeMag>(
                  buildWhen: (p, c) => p.nowTime.minute != c.nowTime.minute,
                  builder: (_, time) => _buildSed(time.nowTime.minute),
                ),
              ),
              //秒
              BlocBuilder<TimeBloc, TimeMag>(
                buildWhen: (p, c) => p.nowTime.second != c.nowTime.second,
                builder: (_, time) => _buildSed(time.nowTime.second),
              ),
            ],
          ),
        ),
      ),
    );
  }

  ///构建分秒
  Widget _buildSed(int time) {
    return AnimatedRotation(
      turn: 6 * time / 360,
      curve: Curves.ease,
      duration: const Duration(milliseconds: 500),
      child: const RoundSed(),
    );
  }

  ///构建时
  Widget _buildHour(int time) {
    return AnimatedRotation(
      turn: 15 * time / 360,
      curve: Curves.ease,
      duration: const Duration(milliseconds: 500),
      child: const RoundHour(),
    );
  }

  ///构建时辰
  Widget _buildShi(int time) {
    return AnimatedRotation(
      turn: ((time + 1) ~/ 2) * 30 / 360,
      curve: Curves.ease,
      duration: const Duration(milliseconds: 500),
      child: Stack(
        alignment: Alignment.center,
        children: [
          const Shichen(),
          const Organ(),
        ],
      ),
    );
  }
}
